package cn.jimmie.lib.qlog.support;

import android.app.ActivityManager;
import android.content.Context;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;

/**
 * Func :
 *
 * <p> Create Time:  2019-06-11 16:41 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public final class AlProcess {
    public static final int PROCESS_FOREGROUND = 0x11;
    public static final int PROCESS_BACKGROUND = 0x22;
    public static final int PROCESS_DISAPPEAR = 0x33;

    public static String getName() {
        try {
            File file = new File("/proc/" + myPid() + "/" + "cmdline");
            BufferedReader mBufferedReader = new BufferedReader(new FileReader(file));
            String processName = mBufferedReader.readLine().trim();
            mBufferedReader.close();
            return processName;
        } catch (Exception e) {
            return null;
        }
    }

    public static int myPid() {
        return android.os.Process.myPid();
    }

    public static boolean isServiceAlive(Context ctx, Class<?> klass) {
        if (ctx == null) return false;
        boolean alive = false;
        try {
            ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningServiceInfo> serviceList = am.getRunningServices(Integer.MAX_VALUE);
            int myUid = android.os.Process.myUid();
            for (ActivityManager.RunningServiceInfo info : serviceList) {
                if (info.uid == myUid && info.service.getClassName().equals(klass.getName())) {
                    alive = true;
                }
            }
        } catch (Exception ignored) {
        }
        return alive;
    }

    public static int uiProcessStatus(Context ctx) {
        return remoteProcessStatus(ctx, null);
    }

    public static int remoteProcessStatus(Context ctx, String remoteName) {
        if (ctx == null) return PROCESS_DISAPPEAR;

        String postfix = TextUtils.isEmpty(remoteName) ? "" : ":" + remoteName;
        String target = ctx.getPackageName() + postfix;
        try {
            ActivityManager am = ((ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE));
            List<ActivityManager.RunningAppProcessInfo> infos = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo info : infos) {
                if (target.equals(info.processName)) {
                    if (info.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
                        return PROCESS_FOREGROUND;
                    else return PROCESS_BACKGROUND;
                }
            }
        } catch (Exception ignored) {
        }
        return PROCESS_DISAPPEAR;
    }

    public static String getProcessName(Context ctx) {
        if (ctx == null) return "unknown";
        try {
            ActivityManager am = ((ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE));
            List<ActivityManager.RunningAppProcessInfo> infos = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo info : infos) {
                if (android.os.Process.myPid() == info.pid) {
                    String name = info.processName;
                    if (name.contains(":")) {
                        return name.substring(name.indexOf(":"));
                    } else {
                        return "host";
                    }
                }
            }
        } catch (Exception ignore) {
        }
        return "unknown";
    }
}